package avicit.bdp.dcs.utils.api.header;

import avicit.bdp.common.dto.api.KeyValue;
import avicit.bdp.dcs.utils.api.Configuration;
import avicit.bdp.dcs.utils.api.enums.BodyParamType;
import avicit.bdp.dcs.utils.api.enums.Key;
import avicit.platform6.core.exception.BusinessException;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.List;

public class PostHeaderAdapter extends HeaderAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(PostHeaderAdapter.class);

    private static PostHeaderAdapter instance;

    private PostHeaderAdapter() {
    }

    public synchronized static PostHeaderAdapter instance() {
        if (instance == null) {
            instance = new PostHeaderAdapter();
        }
        return instance;
    }

    /**
     * 构建Header请求头信息
     * @param readerSliceConfig
     * @return
     */
    public Object buildHttpEntity(Configuration readerSliceConfig){
        // 创建一个头部对象
        HttpHeaders headers = new HttpHeaders();

        List<MediaType> mediaTypeList = new ArrayList<>();
        mediaTypeList.add(MediaType.ALL);
        headers.setAccept(mediaTypeList);

        List<KeyValue> headerList = readerSliceConfig.getList(Key.HEADER, KeyValue.class);
        if (!CollectionUtils.isEmpty(headerList)) {
            for (KeyValue keyValue : headerList) {
                KeyValue k = JSONObject.parseObject(JSONObject.toJSONString(keyValue), KeyValue.class);
                headers.add(k.getName(), k.getValue());
            }
        }

        // 追加权限认证参数
        appendAuthParam(readerSliceConfig, headers);

        Configuration requestParam = readerSliceConfig.getConfiguration(Key.REQUEST_PARAM);
        Configuration bodyParam = requestParam.getConfiguration(Key.BODY_PARAM);
        String bodyParamType = bodyParam.getString(Key.BODY_PARAM_TYPE);
        BodyParamType paramType = BodyParamType.getBodyParamType(bodyParamType);
        switch (paramType) {
            case FORM_DATA:
                headers.setContentType(MediaType.valueOf(MediaType.MULTIPART_FORM_DATA_VALUE));

                MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<>();

                List<KeyValue> formDataList = bodyParam.getList(Key.FORM_DATA, KeyValue.class);
                if (!CollectionUtils.isEmpty(formDataList)) {
                    for(int i = 0; i < formDataList.size(); i++){
                        KeyValue k = JSONObject.parseObject(JSONObject.toJSONString(formDataList.get(i)), KeyValue.class);
                        requestMap.add(k.getName(), k.getValue());
                    }
                }

                return new HttpEntity<MultiValueMap<String, Object>>(requestMap, headers);
            case X_WWW_FORM_URLENCODED:
                headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

                // 提交参数设置
                MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();

                List<KeyValue> xwwwFormUrlencodedList = bodyParam.getList(Key.X_WWW_FORM_URLENCODED, KeyValue.class);
                if (!CollectionUtils.isEmpty(xwwwFormUrlencodedList)) {
                    for(int i = 0; i < xwwwFormUrlencodedList.size(); i++){
                        KeyValue k = JSONObject.parseObject(JSONObject.toJSONString(xwwwFormUrlencodedList.get(i)), KeyValue.class);
                        map.add(k.getName(), k.getValue());
                    }
                }

                return new HttpEntity<MultiValueMap<String, Object>>(map, headers);
            case JSON:
                headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
                String json = bodyParam.getString(Key.JSON);

                return new HttpEntity<Object>(json, headers);
            default:
                String msg = String.format("不支持的BodyParamType:%s", bodyParamType);
                LOG.error(msg);
                throw new BusinessException(msg);
        }
    }

}
